﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using z_tool.config;

namespace z_tool
{
    public partial class col_set : Form
    {
        public col_set()
        {
            InitializeComponent();
        }

        private table_info tbinfo;

        private void col_set_Load(object sender, EventArgs e)
        {
            listBox1.Items.AddRange(baseconfig.tables.Where(s => s.selected == true).ToArray());
            listBox1.DisplayMember = "table_name";
            DataGridViewComboBoxColumn column = dataGridView1.Columns["form_type"] as DataGridViewComboBoxColumn;
            List<itemxx> ixx = new List<itemxx>();
            //表单类型
            ixx.Add(new itemxx() { name = "hidden", value = "hidden" });
            ixx.Add(new itemxx() { name = "input", value = "input" });
            ixx.Add(new itemxx() { name = "inputnumber", value = "inputnumber" });
            ixx.Add(new itemxx() { name = "radio", value = "radio" });
            ixx.Add(new itemxx() { name = "checkbox", value = "checkbox" });
            ixx.Add(new itemxx() { name = "select", value = "select" });
            ixx.Add(new itemxx() { name = "cascader", value = "cascader" });
            ixx.Add(new itemxx() { name = "tree", value = "tree" });
            ixx.Add(new itemxx() { name = "switch", value = "switch" });
            //ixx.Add(new itemxx() { name = "slider", value = "slider" });
            ixx.Add(new itemxx() { name = "timepicker", value = "timepicker" });
            ixx.Add(new itemxx() { name = "datepicker", value = "datepicker" });
            ixx.Add(new itemxx() { name = "datetimepicker", value = "datetimepicker" });
            ixx.Add(new itemxx() { name = "upload", value = "upload" });
            ixx.Add(new itemxx() { name = "img", value = "img" });
            ixx.Add(new itemxx() { name = "video", value = "video" });
            ixx.Add(new itemxx() { name = "imgs", value = "imgs" });
            ixx.Add(new itemxx() { name = "file", value = "file" });
            ixx.Add(new itemxx() { name = "files", value = "files" });
            ixx.Add(new itemxx() { name = "rate", value = "rate" });
            ixx.Add(new itemxx() { name = "textarea", value = "textarea" });
            ixx.Add(new itemxx() { name = "editor", value = "editor" });
            column.DataSource = ixx;
            column.DisplayMember = "name";
            column.ValueMember = "value";
            //复杂列数据来源
            DataGridViewComboBoxColumn column1 = dataGridView1.Columns["parent_info"] as DataGridViewComboBoxColumn;
            List<itemxx> ixx1 = new List<itemxx>();
            ixx1.Add(new itemxx()
            {
                name = "无",
                value = ""
            });
            foreach (table_info ti in baseconfig.tables)
            {
                ixx1.Add(new itemxx()
                {
                    name = ti.table_name,
                    value = ti.table_name
                });
            }
            column1.DataSource = ixx1;
            column1.DisplayMember = "name";
            column1.ValueMember = "value";
        }

        private void col_set_FormClosing(object sender, FormClosingEventArgs e)
        {
            dataGridView1.EndEdit();
            //计算表引用关系
            if (tbinfo != null)
            {
                table_info ckinfo = baseconfig.tables.Where(s => s.table_name == tbinfo.table_name).FirstOrDefault();
                Dictionary<string, string> dic = new Dictionary<string, string>();
                foreach (var column in ckinfo.colums)
                {
                    if (!string.IsNullOrEmpty(column.parent_info))
                    {
                        if (!dic.ContainsKey(column.parent_info) && column.parent_info!=ckinfo.table_name)
                            dic.Add(column.parent_info, "");
                    }
                    if (column.form_type == "editor")
                    {
                        tbinfo.haseditor = true;
                    }
                    if (column.form_type == "img" || column.form_type == "imgs" || column.form_type == "video")
                    {
                        tbinfo.hasimg = true;
                    }
                    if (column.form_type == "file" || column.form_type == "files")
                    {
                        tbinfo.hasfile = true;
                    }
                }
                if (dic.Keys.Count > 0)
                {
                    ckinfo.union_tables = dic.Keys.ToList();
                }
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            tbinfo = listBox1.SelectedItem as table_info;
            if (tbinfo == null) return;
            table_info ckinfo  = baseconfig.tables.Where(s => s.table_name == tbinfo.table_name).FirstOrDefault();
            if (ckinfo != null)
            {
                dataGridView1.DataSource = ckinfo.colums;
            }
        }
    }
}
